# Author    :Dun_Hz
# Time      :2024/3/18 16:02
# 后序表达式计算
from Stack import Stack
def postfix_eval(postfix_express):
    op_stack = Stack()
    token_list = postfix_express.split()
    for token in token_list:
        if token in '0123456789':
            op_stack.push(int(token))       #一定要转换为整数，否则是字符
        else:
            op2 = op_stack.pop()
            op1 = op_stack.pop()
            res = do_math(token, op1, op2)
            op_stack.push(res)
    return op_stack.pop()
def do_math(op, op1, op2):
    if op == '*':
        return op1 * op2
    elif op == '/':
        return round(op1/op2, 3)
    elif op == '+':
        return op1+op2
    else:
        return op1-op2

print(postfix_eval('4 5 6 * +'))